<template>
  <van-datetime-picker
    v-model="currentDate"
    type="date"
    title="选择年月日"
    :min-date="minDate"
    :max-date="maxDate"
    @cancel="$emit('close')"
    @confirm="onConfirm"
  />
</template>

<script>
import { updateUserProfile } from '@/api/user'
import dayjs from 'dayjs'
export default {
  name: 'UpdateBirthday',
  components: {},
  props: {
    value: {
      type: String,
      required: true,
    },
  },
  data() {
    return {
      minDate: new Date(1970, 0, 1), //最小选择支持的日期
      maxDate: new Date(), //最大选择支持的日期
      currentDate: new Date(this.value), //初始显示的选中日期
    }
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    async onConfirm(val) {
      console.log(val)
      this.$toast.loading({
        message: '保存中...',
        forbidClick: true, // 禁止背景点击
        duration: 0, // 持续展示
      })
      //调用接口
      try {
        await updateUserProfile({
          birthday: dayjs(val).format('YYYY-MM-DD'),
        })
        //关闭弹出层
        this.$emit('close')
        //更新视图
        this.$emit('input', dayjs(val).format('YYYY-MM-DD'))
        //提示信息
        this.$toast.success('更新成功')
      } catch (err) {
        this.$toast.fail('更新失败')
      }
    },
  },
}
</script>

<style lang="less" scoped></style>
